package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.service.UserService;
import com.sofwin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2023/5/12 11:22
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper mapper;
    @Override
    public boolean saveOrUpdate(SysUser user) {
        if(user.getId()==null){
            user.setGmtCreate(DateUtil.getNow());
            return mapper.insertSelective(user)==1?true:false;
        }
        user.setGmtModified(DateUtil.getNow());
        return mapper.updateByPrimaryKeySelective(user)==1?true:false;
    }

    @Override
    public SysUser queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeById(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public PageInfo<SysUser> queryByPage(SysUser user, Integer pageNumber, Integer pageSize) {
        PageInfo<SysUser> page = PageHelper.startPage(pageNumber, pageSize).doSelectPageInfo(() -> {
            SysUserExample example = new SysUserExample();
            SysUserExample.Criteria criteria = example.createCriteria();
            if(!StringUtils.isEmpty(user.getLoginName())){
                criteria.andLoginNameLike("%"+user.getLoginName()+"%");
            }
            if(!StringUtils.isEmpty(user.getMobiile())){
                criteria.andMobiileEqualTo(user.getMobiile());
            }
            if(user.getStatus()!=null && user.getStatus()!=-1){
                criteria.andStatusEqualTo(user.getStatus());
            }
            // 创建时间就是开始时间  修改时间就是结束时间
            if(!StringUtils.isEmpty(user.getGmtCreate())){
                criteria.andGmtCreateGreaterThanOrEqualTo(user.getGmtCreate());
            }
            if(!StringUtils.isEmpty(user.getGmtModified())){
                criteria.andGmtCreateLessThanOrEqualTo(user.getGmtModified());
            }
            example.setOrderByClause("id desc");
            mapper.selectByExample(example);
        });
        return page;
    }

    @Override
    public SysUser login(SysUser user) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginNameEqualTo(user.getLoginName());
        List<SysUser> users = mapper.selectByExample(example);
        if(users!=null && users.size()==1){
            return users.get(0);
        }
        return null;
    }
}
